package com.zqxq.lottery.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.DateUtils;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.common.constant.HuaYangConstant;
import com.zqxq.lottery.config.YmlConfig;
import com.zqxq.lottery.entity.po.order.TradeInfoPO;
import com.zqxq.lottery.entity.po.sys.SysCouponPO;
import com.zqxq.lottery.entity.po.user.UserCouponPO;
import com.zqxq.lottery.entity.po.user.UserInfoPO;
import com.zqxq.lottery.entity.po.user.UserMsgPO;
import com.zqxq.lottery.mapper.user.UserCouponMapper;
import com.zqxq.lottery.mapper.user.UserInfoMapper;
import com.zqxq.lottery.mq.Sender;
import com.zqxq.lottery.service.order.ITradeInfoService;
import com.zqxq.lottery.service.sys.ISysCouponService;
import com.zqxq.lottery.service.user.IUserCouponService;
import com.zqxq.lottery.service.user.IUserInfoService;
import com.zqxq.lottery.service.user.IUserMsgService;
import com.zqxq.lottery.service.user.JPushMsgService;
import com.zqxq.lottery.util.DateUtil;
import com.zqxq.lottery.util.HttpUtils;
import com.zqxq.lottery.util.JPushUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;

/**
 * @author 苦智
 * @date 2018/6/22 15:19
 * @description 极光推送
 * @ClassName JPushMsgController
 */
@RestController
@Slf4j
@RequestMapping("/jPushMsg")
public class JPushMsgController {
    private static Map<String,Object> headerMap = new HashMap<String, Object>(){{
        put("Authorization","Basic Y2UxZmViNDhiNDljNTE1ZDRhMWIzMzY5OjE4YzAwYjdkZDY5OWM5MjVjMWNlZDhmNw==");
    }};

    @Autowired
    private IUserMsgService userMsgService;
    @Autowired
    private IUserInfoService userInfoService;

    @PostMapping("/pushMsg")
    public Result pushMsgToUser(String target, String title,String content,Integer type){
        log.info("极光推送 mobile={},msg={},content={},type={}",target,title,content,type);
        if (StrUtils.isEmpty(target) || StrUtils.isEmpty(content) || type == null) {
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        }
        Map<String,Object> map = new HashMap<>();
        String msgIds = "";
        List<UserMsgPO> msgList = new ArrayList<>();
        int tag = 1;
        Map<String,Object> resultMap = new HashMap<>();

        if (StrUtils.isNotEmpty(title)){
            content = "【" + title + "】" + content;
        }
        try {
            if (StrUtils.isNotEmpty(target) && !"ALL".equals(target) ) {
                String[] phones = target.split(",");
                for (String phone : phones) {
                    if (type == 1){
                        //极光推送+站内通知
                        map = JPushUtil.pushToOne(phone, content);
                        if (phones.length > tag){
                            msgIds+=map.get("msgId")+",";
                        }else{
                            msgIds+=map.get("msgId");
                        }
                        tag++;
                    }
                    Wrapper<UserInfoPO> wrapper = new EntityWrapper<>();
                    wrapper.eq("mobile",phone);
                    UserInfoPO userInfoPO = userInfoService.selectOne(wrapper);
                    UserMsgPO userMsgPO = new UserMsgPO();
                    userMsgPO.setUser_id(userInfoPO.getId());
                    userMsgPO.setContent(content);
                    userMsgPO.setType(4);
                    msgList.add(userMsgPO);
                }
                //插入用户消息列表
                userMsgService.insertBatch(msgList);
            } else {
                if (type == 1){
                    map = JPushUtil.pushToAll(content);
                    msgIds = map.get("msgId").toString();
                }
                //把通知插入用户消息表
                userMsgService.insertBatchMsg(content);
            }
            resultMap.put("msgIds",msgIds);
        }catch (Exception e){
            log.error("极光推送失败",e);
            return new Result(CodeEnum.FAIL.getCode(),"极光推送失败");
        }
        return new Result(CodeEnum.SUCCESS.getCode(),"推送成功",resultMap);
    }

    @PostMapping("/getPushResult")
    public Result getPushResult(String msgIds){
        if (StrUtils.isEmpty(msgIds)){
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        }
        String s = HttpUtils.doGet("https://report.jpush.cn/v3/received?msg_ids=" + msgIds, headerMap);
        return new Result(CodeEnum.SUCCESS.getCode(),"查询查询成功",JSONObject.parse(s));
    }

}
